package com.warren.biz.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.warren.annotation.ControllerEndpoint;
import com.warren.biz.entity.Consumer;
import com.warren.biz.service.ConsumerService;
import com.warren.biz.vo.SupplierVO;
import com.warren.response.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;

import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author warren
 * @since 2021-04-20
 */
@RestController
@RequestMapping("/biz/consumer")
@Api(value = "物资去向接口模块",tags = "物资去向接口模块")
public class ConsumerController {

    @Resource
    private ConsumerService consumerService;

    @ApiOperation(value = "分页查询物资去处")
    @PostMapping("/findConsumerList")
    public Result findConsumerList(@RequestParam(defaultValue = "1") Integer current,
                                   @RequestParam(defaultValue = "6") Integer size,
                                   @RequestBody SupplierVO supplierVO){
        //取得分页条件
        Page<Consumer> page = new Page<>(current,size);
        QueryWrapper<Consumer> wrapper = getWrapper(supplierVO);
        //单表查询，直接套用supplierVO跟consumerVO类似
        Page<Consumer> productPage = consumerService.page(page, wrapper);

        long total = productPage.getTotal();
        List<Consumer> records = productPage.getRecords();
        return Result.ok().data("total",total).data("records",records);
    }

    @ApiOperation(value = "获取物资所有去处")
    @GetMapping("/getAllConsumerList")
    public Result getAllConsumerList(){
        List<Consumer> consumers = consumerService.list();
        return Result.ok().data("records",consumers);
    }

    @ControllerEndpoint(exceptionMessage = "添加去处失败",operation = "添加去处")
    @ApiOperation(value = "添加去处")
    @PostMapping("/addConsumer")
    public Result addConsumer(@RequestBody Consumer consumer){
        consumerService.save(consumer);
        return Result.ok();
    }

    @ControllerEndpoint(exceptionMessage = "修改去处失败",operation = "修改去处")
    @ApiOperation(value = "修改去处")
    @PostMapping("/updateConsumer")
    public Result updateConsumer(@RequestBody Consumer consumer){
        consumerService.updateById(consumer);
        return Result.ok();
    }

    @ControllerEndpoint(exceptionMessage = "删除去处失败",operation = "删除去处")
    @ApiOperation(value = "删除去处")
    @DeleteMapping("/deleteConsumerById")
    public Result deleteConsumerById(String id){
        consumerService.removeById(id);
        return Result.ok();
    }

    private QueryWrapper<Consumer> getWrapper(SupplierVO supplierVo) {
        QueryWrapper<Consumer> queryWrapper = new QueryWrapper<>();
        if (supplierVo != null) {
            //查询每个条件
            if (!ObjectUtils.isEmpty(supplierVo.getName())) {
                queryWrapper.like("name", supplierVo.getName());
            }
            if (!ObjectUtils.isEmpty(supplierVo.getAddress())) {
                queryWrapper.like("address", supplierVo.getAddress());
            }
            if (!ObjectUtils.isEmpty(supplierVo.getContact())) {
                queryWrapper.like("contact", supplierVo.getContact());
            }
        }
        return queryWrapper;
    }
}

